package com.zhongxi.guoyuan.view.personalCenter;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.adapter.personCenter.BeanDynamicAdapter;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.BaseActivity;
import com.zhongxi.guoyuan.entity.BaseResponse;
import com.zhongxi.guoyuan.entity.user.center.BeanDynamicResult;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.utils.CommonTool;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class BeanDynamicActivity extends BaseActivity {


    @Bind(R.id.ivBack)
    ImageView ivBack;
    @Bind(R.id.tvTitle)
    TextView tvTitle;
    @Bind(R.id.bean_dynamic_hongdou)
    TextView beanDynamicHongdou;
    @Bind(R.id.bean_dynamic_meidou)
    TextView beanDynamicMeidou;
    @Bind(R.id.bean_dynamic_numberTip)
    TextView beanDynamicNumberTip;
    @Bind(R.id.empty_text)
    TextView emptyText;
    @Bind(R.id.bean_dynamic_recycleview)
    RecyclerView beanDynamicRecycleview;

    @Bind(R.id.bean_dynamic_startTime)
    TextView beanDynamicStartTime;
    @Bind(R.id.bean_dynamic_endTime)
    TextView beanDynamicEndTime;
    @Bind(R.id.bean_dynamic_search)
    ImageView beanDynamicSearch;
    private BeanDynamicResult result;

    private BeanDynamicAdapter adapter;
    private boolean isLoading;
    private LinearLayoutManager layoutManager;
    private int lastItem;

    private List<BeanDynamicResult.BeanDynamicData> hongdouList;

    private int beanType = 1;
    private boolean nomore;


    private DatePickerDialog dialog;
    private String startTime="";
    private String endTime="";
    private String dateNowStr;

    boolean issearch=false;
    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.activity_bean_dynamic);
        ButterKnife.bind(this);
        initTitle();
        tvTitle.setText("豆豆动态");
        beanDynamicHongdou.setSelected(true);
        beanDynamicMeidou.setSelected(false);
    }

    @Override
    protected void initData() {
        super.initData();
        hongdouList = new ArrayList<>();
        layoutManager = new LinearLayoutManager(this);
        beanDynamicRecycleview.setLayoutManager(layoutManager);
        adapter = new BeanDynamicAdapter(this);
        beanDynamicRecycleview.setAdapter(adapter);
        adapter.setFooter_state(0);

        initTime();
    }

    private void initTime() {
        getData();
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        dateNowStr = sdf.format(now);
        beanDynamicStartTime.setText(dateNowStr+"  ");
        beanDynamicEndTime.setText(dateNowStr+"  ");
        startTime = endTime = dateNowStr;


    }

    @Override
    protected void initEvent() {
        super.initEvent();
        beanDynamicRecycleview.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastItem + 1 == adapter.getItemCount() && !isLoading) {
                    //滑动到底部加载更多
                    if (!nomore) {
                        pageNum++;
                        getData();
                        isLoading = true;
                        adapter.setFooter_state(1);
                    }
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastItem = layoutManager.findLastVisibleItemPosition();
            }
        });
    }
    String str="";
    private void getData() {
        if (issearch){
             str = "&page=" + pageNum + "&stime=" + startTime + "&etime=" + endTime;
        }else {
             str = "&page=" + pageNum;
        }

        getServer(Url.BEAN_DYNAMIC, null, str, false, null, "get");
    }


    @Override
    protected void processSuccessResult(String response) {
        super.processSuccessResult(response);
        Gson gson = new Gson();
        result = gson.fromJson(response, BeanDynamicResult.class);
        MyApplication.getInstance().setSpValue(result.getSafeobj());

        if (pageNum == 1) {
            hongdouList.clear();
        }
        if (null != result && !"".equals(result) && result.getData().size() > 0) {
            emptyText.setVisibility(View.GONE);
            hongdouList.addAll(result.getData());

            adapter.setData(hongdouList);
            nomore = false;
        } else {
            if (pageNum > 1) {
                pageNum--;
                //没有更多了
                nomore = true;
            } else {
                pageNum = 1;
                emptyText.setVisibility(View.VISIBLE);
            }
        }

        isLoading = false;
        adapter.setFooter_state(0);

    }

    @Override
    protected void ProcessFailResult(String response) {
        super.ProcessFailResult(response);
        Gson gson = new Gson();
        BaseResponse baseResult = gson.fromJson(response, BaseResponse.class);
        MyApplication.getInstance().setSpValue(baseResult.getSafeobj());
        adapter.setFooter_state(0);
        if (pageNum > 1) {
            pageNum--;
        }
        if (baseResult.getCode() == 1000) {
            showShortToast("参数错误");
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @OnClick({R.id.ivBack, R.id.bean_dynamic_hongdou, R.id.bean_dynamic_meidou, R.id.bean_dynamic_search, R.id.bean_dynamic_startTime, R.id.bean_dynamic_endTime})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ivBack:
                onBackPressed();
                break;
            case R.id.bean_dynamic_hongdou:
                beanDynamicHongdou.setSelected(true);
                beanDynamicMeidou.setSelected(false);
                beanDynamicNumberTip.setText("数量(红豆)");
                pageNum = 1;
                getData();
                beanType = 1;
                adapter.notifyDataSetChanged();
                break;
            case R.id.bean_dynamic_meidou:
                beanDynamicMeidou.setSelected(true);
                beanDynamicHongdou.setSelected(false);
                beanDynamicNumberTip.setText("数量(美豆)");
                pageNum = 1;
                getData();
                beanType = 2;
                adapter.notifyDataSetChanged();
                break;
            case R.id.bean_dynamic_startTime:
                showDateDialog(0);
                break;
            case R.id.bean_dynamic_endTime:
                showDateDialog(1);
                break;
            case R.id.bean_dynamic_search:
                issearch=true;
                pageNum = 1;
                getData();
                break;
        }
    }

    private void showDateDialog(final int index) {
        Calendar c = Calendar.getInstance();
        dialog = new DatePickerDialog(this, AlertDialog.THEME_HOLO_LIGHT, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                String mon = month + 1 + "";
                String text = year + "-" + mon + "-" + dayOfMonth;

                text = CommonTool.TimeCompare(text, dateNowStr) ? text : dateNowStr;
                if (index == 0) {
                    beanDynamicStartTime.setText(text+"  ");
                    startTime = text;
                } else if (index == 1) {
                    beanDynamicEndTime.setText(text+"  ");
                    endTime = text;
                }
            }
        }, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
        dialog.setTitle("请选择时间：");
        dialog.getDatePicker().setCalendarViewShown(false);
        dialog.show();
    }

}
